package cn.sort;

//寻找第一个相交的节点
public class FindFirstIntersectNode {
    public static class Node{
        public int value;
        public Node next;
        public Node(int data){
            this.value = data;
        }
    }


    //如果相交的话返回第一个相交的节点,否则返回null
    public static Node getIntersectNode(Node head1,Node head2){
        if(head1 == null || head2 == null){
            return null;
        }
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);
        if (loop1 == null && null == loop2) {
            //两个无环链表的相交问题
            return noloop(head1,head2);
        }
        if(loop1 != null && loop2 != null){
            //两个有环链表的相交问题
            return bothLoop(head1,loop1,head2,loop2);
        }
        return  null;
    }

    private static Node noloop(Node head1, Node head2) {
        if(head1 == null || head2 == null) {
            return null;
        }
        Node cur1 = head1;
        Node cur2 = head2;
        int n = 0;
        while (cur1.next != null){
            n++;
            cur1 = cur1.next;
        }
        while (cur2.next != null) {
            n--;
            cur2 = cur2.next;
        }
        if(cur1 != cur2){
            return null;
        }
        cur1 = n>0?head1:head2;//cur1 是较长的
        cur2 = cur1 == head1?head2:head1; // cur2是较短的

        n = Math.abs(n);
        while (n!=0){
            n--;
            cur1 = cur1.next;
        }

        while (cur1 != cur2){
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    private static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node cur1 = null;
        Node cur2 = null;
        //第二种情况
        if (loop1 == loop2) {
            cur1 = head1;
            cur2 = head2;
            int n =0;
            while (cur1 != loop1){
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2){
                n--;
                cur2 = cur2.next;
            }
            cur1 = n>0?head1:head2;
            cur2 = cur1 == head1?head2:head1;
            n = Math.abs(n);
            while (n!=0){
                n--;
                cur1 = cur1.next;
            }
            while (cur1 != cur2){
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        } else { // 情况1 和情况3
            cur1 = loop1.next;
            while (cur1 != loop2){
                if (cur1 == loop2) {
                    return loop1;
                }
                cur1 = cur1.next;
            }
            return null;

        }
    }

    //获取入环的第一个节点
    private static Node getLoopNode(Node head) {
        //构不成一个环
        if(head == null ||head.next == null || head.next.next == null){
            return null;
        }
        Node n1 = head.next;//n1 -> slow
        Node n2 = head.next.next; //n2 -> fast
        while (n1 != n2) {
            //如果快指针在遇到n1前为空,那么不会有环
            if (n2.next == null || n2.next.next == null) {
                return null;
            }
            n2 = n2.next.next;
            n1 = n1.next;
        }
        n2 = head;//返回头
        while (n1 != n2){
            n1 = n1.next;
            n2 = n2.next;
        }
        return n1;
    }
}
